<?php
class keu_kurs_model extends MY_Model {
	var $model = 'keu_kurs';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT A.KD_VALUTA, B.NAMA_VALUTA, TO_CHAR(A.TGL_EFEKTIF,'DD/MM/YYYY') TGL_EFEKTIF, A.HARGA_BELI, A.HARGA_JUAL, A.HARGA_RATA
					FROM KEU_KURS A JOIN SW_VALUTA B ON A.KD_VALUTA=B.KD_VALUTA 								
					WHERE	UPPER(B.KD_VALUTA) LIKE UPPER('%".post('keyword')."%')
							OR UPPER(B.NAMA_VALUTA) LIKE UPPER('%".post('keyword')."%')
							OR A.TGL_EFEKTIF LIKE '%".post('keyword')."%'
					ORDER BY ".(post('sort')=='' ? 'B.KD_VALUTA, A.TGL_EFEKTIF' : post('sort'))." ".(post('order')=='' ? 'ASC' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('KEU_KURS', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('KEU_KURS', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_codecurrency, $as_effdate){
		$result = $this->db->get_where('KEU_KURS', array('KD_VALUTA' => $as_codecurrency, 'TGL_EFEKTIF' => date_format3($as_effdate,'YYYY-MM-DD')));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_codecurrency, $as_effdate)
	{
		$result = $this->db->query("SELECT	a.KD_VALUTA, b.NAMA_VALUTA, a.TGL_EFEKTIF, A.HARGA_BELI, A.HARGA_JUAL, A.HARGA_RATA
									FROM	KEU_KURS a JOIN SW_VALUTA b ON a.KD_VALUTA=b.KD_VALUTA			
									WHERE	KD_VALUTA = '".$as_codecurrency." AND TGL_EFEKTIF = '".date_format3($as_effdate,'YYYY-MM-DD')."'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function DeleteSingle($as_codecurrency, $as_effdate)
	{
		try {
			$this->db->delete('KEU_KURS', array('KD_VALUTA' => $as_codecurrency, 'TGL_EFEKTIF' => date_format3($as_effdate,'YYYY-MM-DD')));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>